package com.cardbaobao.cardbabyclient.ui.pop;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.widget.CustomPwdView;


/**
 * Description:
 * Created by FHW
 * on 2017/11/10
 */

public class NumberKeyInputPwdPop extends PopupWindow {
    private OnKeyFinish listener;

    private CustomPwdView v_pwd;
    private TextView tv_input_count;

    private String tranKey = "";//密码
    private String num = "";//数字

    public NumberKeyInputPwdPop(Context context) {
        View root = LayoutInflater.from(context).inflate(R.layout.pop_number_keyboard_input_pwd, null);
        root.findViewById(R.id.key_delete).setOnClickListener(clickListener);
        root.findViewById(R.id.key_one).setOnClickListener(clickListener);
        root.findViewById(R.id.key_two).setOnClickListener(clickListener);
        root.findViewById(R.id.key_three).setOnClickListener(clickListener);
        root.findViewById(R.id.key_four).setOnClickListener(clickListener);
        root.findViewById(R.id.key_five).setOnClickListener(clickListener);
        root.findViewById(R.id.key_six).setOnClickListener(clickListener);
        root.findViewById(R.id.key_seven).setOnClickListener(clickListener);
        root.findViewById(R.id.key_eight).setOnClickListener(clickListener);
        root.findViewById(R.id.key_nine).setOnClickListener(clickListener);
        root.findViewById(R.id.key_zero).setOnClickListener(clickListener);
        root.findViewById(R.id.key_close).setOnClickListener(clickListener);
        root.findViewById(R.id.tv_forget_pwd).setOnClickListener(clickListener);

        v_pwd = (CustomPwdView) root.findViewById(R.id.v_pwd);

        tv_input_count = (TextView) root.findViewById(R.id.tv_input_count);

        setContentView(root);
        setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new ColorDrawable());
        setOutsideTouchable(false);
        setAnimationStyle(R.style.CustomBottomAnimateStyle);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            StringBuilder sb = new StringBuilder(tranKey);
            boolean isFinsh = false;
            switch (v.getId()) {
                case R.id.key_delete:
                    //删除键
                    if (sb.length() > 0) {
                        sb.deleteCharAt(sb.length() - 1);
                    }
                    break;
                case R.id.key_one:
                    num = "1";
                    sb.append(num);
                    break;
                case R.id.key_two:
                    num = "2";
                    sb.append(num);
                    break;
                case R.id.key_three:
                    num = "3";
                    sb.append(num);
                    break;
                case R.id.key_four:
                    num = "4";
                    sb.append(num);
                    break;
                case R.id.key_five:
                    num = "5";
                    sb.append(num);
                    break;
                case R.id.key_six:
                    num = "6";
                    sb.append(num);
                    break;
                case R.id.key_seven:
                    num = "7";
                    sb.append(num);
                    break;
                case R.id.key_eight:
                    num = "8";
                    sb.append(num);
                    break;
                case R.id.key_nine:
                    num = "9";
                    sb.append(num);
                    break;
                case R.id.key_zero:
                    num = "0";
                    sb.append(num);
                    break;
                case R.id.tv_forget_pwd:
                    listener.onKeyFinish("");
                    break;
                case R.id.key_close:
                    isFinsh = true;
                    dismiss();
                    break;
            }
            //限制6位验证码
            if (sb.length() > 6) {
                tranKey = sb.substring(0, 6);
            } else {
                tranKey = sb.toString();
            }
//            LogUtil.d(tranKey);
            if (tranKey.length() >= 6) {
                if (listener != null && !isFinsh) {
                    listener.onKeyFinish(tranKey);
                }
            }
            if (tranKey.length() > 0) {
                v_pwd.setShowNum(tranKey.length());
            } else {
                v_pwd.setShowNum(0);
            }

        }
    };

    @Override
    public boolean isShowing() {
        tranKey = "";
        if (v_pwd != null)
            v_pwd.setShowNum(0);
        return super.isShowing();
    }

    public void cleanNum() {
        tranKey = "";
        if (v_pwd != null) {
            v_pwd.setShowNum(0);
        }
    }

    public void setNumberKeyListener(OnKeyFinish listener) {
        this.listener = listener;
    }

    public interface OnKeyFinish {
        void onKeyFinish(String tranKey);

    }

    public void setErrorText(String num) {
        if (tv_input_count != null) {
            tv_input_count.setVisibility(View.VISIBLE);
            tv_input_count.setText(Html.fromHtml("还有<font color='#FE1211'>" + num + "</font>次输入机会"));
        }
    }

    public void hidErrorView() {
        if (tv_input_count != null) {
            tv_input_count.setVisibility(View.GONE);
        }
    }

}
